$(function () {
    //初始化专业搜索条件
    $.initSubject({
        element:'subject-box',
        confirmBtn:false,
        removeBtn: false,
        style:false,
        notMultiple:true,
        classText:'form-control mb10',
        width:100,
        selectCallback:function(subjectIds){
            $("#subjectIds").val(subjectIds);
        }
    });

    $("body").on('click', '#btn1,#btn2', function () { //搜索课程
        listBookData(1);
    }).on('click','#oc-ibox-content i.fa-eye',function () {//查看详情
        var id = $(this).parent().data('id');
        parent.layer.open({
            area: winArea,
            btn: ['进入修改状态', '取消'],
            content: baseloaction + "/admin/book/bookInfo?id=" + id,
            maxmin: true,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '查看图书  [<small>' + $(this).parents('tr').data('name') + '</small>]',
            type: 2,
            yes: function (index) {
                window.location = baseloaction + '/admin/book/initUpdate?id=' + id ;
                parent.layer.close(index);
            }
        });
    }).on('click','#oc-ibox-content i.fa-arrow-down',function () {//单个下架
        var id = $(this).parent().data("id");
        icModale(2,'确认要下架该图书？',3,function () {
            updateBookStatus(id,2);
        });
    }).on('click','#oc-ibox-content i.fa-arrow-up',function () {//单个上架
        var id = $(this).parent().data("id");
        icModale(2,'确认要上架该图书？',3,function () {
            updateBookStatus(id,1);
        });
    }).on('click','#oc-ibox-content i.fa-trash',function () {//单个删除
        var id = $(this).parent().data("id");
        icModale(2,'确认要删除该图书？',3,function () {
            updateBookStatus(id,3);
        });
    }).on('click','#onBatch',function () {//批量上架
        var ids = getIds();
        if(ids==null){
            icModale(0,"请选择要上架的图书");
            return;
        }
        icModale(2,'确认要上架选中图书？',3,function () {
            updateBookStatus(ids,1);
        });
    }).on('click','#offBatch',function () {//批量下架
        var ids = getIds();
        if(ids==null){
            icModale(0,"请选择要下架的图书");
            return;
        }
        icModale(2,'确认要下架选中图书？',3,function () {
            updateBookStatus(ids,2);
        });
    }).on('click','#deleteBatch',function () {//批量删除
        var ids = getIds();
        if(ids==null){
            icModale(0,"请选择要删除的图书");
            return;
        }
        icModale(2,'确认要删除选中图书？',3,function () {
            updateBookStatus(ids,3);
        });
    });

    /**
     * 修改图书状态
     * @param ids 图书ID串
     * @param status 状态,1上架，2下架，3删除
     */
    function updateBookStatus(ids,status) {
        $.ajax({
            url:baseloaction +'/admin/book/updateBookStatus',
            type:'post',
            dataType:'json',
            data:{
                'ids':ids,
                'status':status
            },
            success:function (result) {
                if(result.success==true){
                    var arr = (ids+'').split(",");
                    $.each(arr,function (index,id) {
                       if(status==3){
                           $("#book-"+id).remove();
                       }
                       var statusEm = $("#status-"+id);
                       var statusAction = $("#status-action-"+id);
                       if(status==1){
                           statusEm.html('<span class="label label-info">上架</span>');
                           statusAction.html('<i class="fa fa-arrow-down"></i>');
                           statusAction.attr('data-original-title','下架');
                       }else if(status==2){
                           var _status = statusEm.data('status');
                           if(_status!=0){//初始状态的图书不可下架
                               statusEm.html('<span class="label label-danger">下架</span>');
                               statusAction.html('<i class="fa fa-arrow-up"></i>');
                               statusAction.attr('data-original-title','上架');
                           }
                       }
                    });
                }else{
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,"系统错误，操作失败");
            }
        });
    }


    listBookData(1);
    /**
     * 加载第 pageNo 页数据
     * @param pageNo
     */
    function listBookData(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/book/ajaxBookList',//请求URL
            contextId: 'oc-ibox-content',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: listBookData, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-book', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").text('（共'+total+'种图书）');
            }
        };
        ajaxGetListData(obj);
    }

    /**
     * 获取选中的数据
     * @returns 图书ID串
     */
    function getIds() {
        var list = $('#oc-ibox-content input.i-checks:checked');
        if(list==null || list.length<=0){
            return null;
        }
        return list.map(function () {
            return $(this).parents('tr').data('id');
        }).get().join(',');
    }
});